from hashlib import sha256

class OAEP():
	def enc(self, M):
		if(len(M) > 350):  raise NameError("OAEP: enc(): 'M' too large")
		padlen = 350 - len(M)
		D = '\x00'*padlen + '\x01' + M
		R   = os.urandom(32)
		hR  = lib().mgf(sha256, R, 351)
		D1  = lib().strxor(hR, D)
		hD1 = sha256(D1).digest()
		R1  = lib().strxor(hD1, R)
		return '\x00' + R1 + D1

	def dec(self, C):
		invalid = NameError("OAEP: dec(): 'C' is invalid")
		if len(C) != 384 :  raise NameError("OAEP: dec(): 'C' length is invalid")
		if C[0] != '\x00':  return False
		R1  = C[1:33]
		D1  = C[33: ]
		hD1 = sha256(D1).digest()
		R   = lib().strxor(hD1, R1)
		hR  = lib().mgf(sha256, R, 351)
		D   = lib().strxor(hR, D1)
		D = D.lstrip("\x00")
		if(D[0] != '\x01'): return False
		return D[1:]